package cn.itcast.sailing.sms.qcloud;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20190711.models.SendSmsResponse;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 扩展腾讯云发送短信方法
 * 因为腾讯云引用json的包与spring-boot有冲突，故自己来实现
 */
public class QCloudSmsSingleSender extends SmsSingleSender {
	@Value("${sms.qcloud.appId}")
	private String appId;
	@Value("${sms.qcloud.appKey}")
	private String appKey;
	@Value("${sms.qcloud.templateId}")
	private String templateId;
	@Value("${sms.qcloud.sign}")
	private String sign;


	private String url = "https://yun.tim.qq.com/v5/tlssmssvr/sendsms";

	QCloudSmsSingleSender(int appid, String appkey) {
		super(appid, appkey);
	}

	@Override
	public SmsSingleSenderResult sendWithParam(String nationCode, String phoneNumber, int templateId, String[] params,
			String sign, String extend, String ext) throws HTTPException, JSONException, IOException {
		return this
				.sendWithParam(nationCode, phoneNumber, templateId, new ArrayList(Arrays.asList(params)), sign, extend,
						ext);
	}

	@Override
	public SmsSingleSenderResult sendWithParam(String nationCode, String phoneNumber, int templateId,
			ArrayList<String> params, String sign, String extend, String ext)
			throws HTTPException, JSONException, IOException {
//		long random = SmsSenderUtil.getRandom();
//		long now = SmsSenderUtil.getCurrentTime();
//		//扩展方法 使用fastjson
//		JSONObject body = new JSONObject();
//		Map telMap = new HashMap<>();
//		telMap.put("mobile", phoneNumber);
//		telMap.put("nationcode", nationCode);
//		body.put("tel", telMap);
//		body.put("params", params);
//		body.put("tpl_id", templateId);
//		body.put("sig", SmsSenderUtil.calculateSignature(this.appkey, random, now, phoneNumber));
//		body.put("sign", sign);
//		body.put("time", now);
//		body.put("extend", SmsSenderUtil.isNotEmpty(extend) ? extend : "");
//		body.put("ext", SmsSenderUtil.isNotEmpty(ext) ? ext : "");
//
//		HTTPRequest req = (new HTTPRequest(HTTPMethod.POST, this.url)).addHeader("Conetent-Type", "application/json")
//				.addQueryParameter("sdkappid", this.appid).addQueryParameter("random", random)
//				.setConnectionTimeout('\uea60').setRequestTimeout('\uea60').setBody(body.toString());
//
//		try {
//			HTTPResponse e = this.httpclient.fetch(req);
//			this.handleError(e);
//			return (new SmsSingleSenderResult()).parseFromHTTPResponse(e);
//		} catch (URISyntaxException var15) {
//			throw new RuntimeException("API url has been modified, current url: " + this.url);
//		}
		try{

			Credential cred = new Credential(appId, appKey);

			HttpProfile httpProfile = new HttpProfile();
			httpProfile.setEndpoint("sms.tencentcloudapi.com");

			ClientProfile clientProfile = new ClientProfile();
			clientProfile.setHttpProfile(httpProfile);

			SmsClient client = new SmsClient(cred, "", clientProfile);

			SendSmsRequest req = new SendSmsRequest();
			String[] phoneNumberSet1 = {phoneNumber};
			req.setPhoneNumberSet(phoneNumberSet1);

			req.setTemplateID(""+templateId);
			req.setSign(sign);
			req.setSmsSdkAppid(appId);

			SendSmsResponse resp = client.SendSms(req);
			System.out.println(SendSmsResponse.toJsonString(resp));
			return null;
//			return (new SmsSingleSenderResult()).parseFromHTTPResponse(resp);

		} catch (TencentCloudSDKException e) {
			System.out.println(e.toString());
		}
		return null;
	}
}